﻿using KYJScadaProModbusRtu;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KYJScadaProModbusRTU
{
    public partial class FrmParamSel : Form
    {
        public FrmParamSel(List<string> selectParam)
        {
            InitializeComponent();

            if(CommomMethods.plcDevice != null)
            {
                UnSelectParamList = CommomMethods.plcDevice.StoreVarList.Select(c => c.Comments).ToList();

                foreach(string item in selectParam)
                {
                    SelectParamList.Add(item);

                    UnSelectParamList.Remove(item);
                }
                UpdateListBox();
            }
        }

        //未选择参数的集合
        private List<string> UnSelectParamList = new List<string>();

        //已选择参数的集合
        public List<string> SelectParamList = new List<string>();

        private void UpdateListBox()
        {
            this.lst_SelectList.Items.Clear();
            this.lst_SelectList.Items.AddRange(SelectParamList.ToArray());

            this.lst_UnSelectList.Items.Clear();
            this.lst_UnSelectList.Items.AddRange(UnSelectParamList.ToArray());
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            //如果没有数据或者没选数据
            if(this.UnSelectParamList.Count == 0 || this.lst_UnSelectList.SelectedItem == null)
            {
                return;
            }

            int index = this.lst_UnSelectList.SelectedIndex;

            string value = this.lst_UnSelectList.Items[index].ToString();

            this.UnSelectParamList.Remove(value);
            this.SelectParamList.Add(value);

            UpdateListBox();

            //如果选中状态
            if(this.lst_UnSelectList.Items.Count > index - 1)
            {
                if(index == 0)
                {
                    this.lst_UnSelectList.SetSelected(0, true);
                }
                else
                {
                    this.lst_UnSelectList.SetSelected(index - 1, true);
                }
            }
        }

        private void btn_AddAll_Click(object sender, EventArgs e)
        {
            //如果没有数据
            if (this.UnSelectParamList.Count == 0)
            {
                return;
            }

            for (int i = 0; i < this.UnSelectParamList.Count; i++)
            {
                this.SelectParamList.Add(this.UnSelectParamList[i]);
            }

            this.UnSelectParamList.Clear();

            UpdateListBox();

            if (this.lst_SelectList.Items.Count > 0)
            {
                this.lst_SelectList.SetSelected(0, true);
            }
        }

        private void btn_Del_Click(object sender, EventArgs e)
        {
            //如果没有数据或者没选数据
            if (this.SelectParamList.Count == 0 || this.lst_SelectList.SelectedItem == null)
            {
                return;
            }

            int index = this.lst_SelectList.SelectedIndex;

            string value = this.lst_SelectList.Items[index].ToString();

            this.SelectParamList.Remove(value);
            this.UnSelectParamList.Add(value);

            UpdateListBox();

            //如果选中状态
            if (this.lst_SelectList.Items.Count > index - 1)
            {
                if(index == 0)
                {
                    this.lst_SelectList.SetSelected(0, true);
                }
                else
                {
                    this.lst_SelectList.SetSelected(index - 1, true);
                }
            }
        }

        private void btn_DelAll_Click(object sender, EventArgs e)
        {
            //如果没有数据
            if (this.SelectParamList.Count == 0)
            {
                return;
            }

            for (int i = 0; i < this.SelectParamList.Count; i++)
            {
                this.UnSelectParamList.Add(this.SelectParamList[i]);
            }

            this.SelectParamList.Clear();

            UpdateListBox();

            if (this.lst_UnSelectList.Items.Count > 0)
            {
                this.lst_UnSelectList.SetSelected(0, true);
            }
        }

        private void btn_Select_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btn_Canel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        #region//窗体拖动
        public static Point CPoint;

        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            CPoint = new Point(-e.X, -e.Y);
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point myPosittion = Control.MousePosition;//获取当前鼠标的屏幕坐标
                myPosittion.Offset(CPoint.X, CPoint.Y);//重载当前鼠标的位置
                this.DesktopLocation = myPosittion;//设置当前窗体在屏幕上的位置
            }
        }
        #endregion

        #region 减少闪烁
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams parss = base.CreateParams;
                parss.ExStyle |= 0x02000000;
                return parss;
            }
        }
        #endregion
    }
}
